﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


public class DBClass
{
    SqlConnection myConn;    
    SqlCommand myCmd;        
    DataSet ds;             
    SqlDataAdapter adapt;    
	public DBClass()
	{

	}
 
    public SqlConnection GetConnection()
    {
        string myStr = ConfigurationManager.AppSettings["connectionString"].ToString();
        myConn = new SqlConnection(myStr);
        return myConn;
    }
 
    public void ExecNonQuery(string strSql)
    {
        
        try
        {
            myConn = GetConnection();
            myCmd = new SqlCommand();
            myCmd.Connection = myConn;
            myCmd.CommandText = strSql;
            if (myCmd.Connection.State != ConnectionState.Open)
            {
                myCmd.Connection.Open();
            }
            myCmd.ExecuteNonQuery();

        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message, ex);

        }
        finally
        {
            if (myCmd.Connection.State == ConnectionState.Open)
            {
                myCmd.Connection.Close();
                myConn.Dispose();
                myCmd.Dispose();

            }
        }
    }
   
 
    public string ExecScalar(string strSql)
    {
     
        try
        {
            myConn = GetConnection();
            myCmd = new SqlCommand();
            myCmd.Connection = myConn;
            myCmd.CommandText = strSql;
            if (myCmd.Connection.State != ConnectionState.Open)
            {
                myCmd.Connection.Open();
            }
           
            strSql=Convert.ToString(myCmd.ExecuteScalar());
            return strSql ;

        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message, ex);

        }
        finally
        {
            if (myCmd.Connection.State == ConnectionState.Open)
            {
                myCmd.Connection.Close();
                myConn.Dispose();                
                myCmd.Dispose();

            }
        }
    
    }

    public DataTable GetDataSet(string strSql, string TableName)
    {
        ds= new DataSet();
        try
        {
            myConn = GetConnection();
            adapt = new SqlDataAdapter(strSql, myConn); 
            adapt.Fill(ds,TableName);
            return ds.Tables[TableName];

        }
                    catch (Exception ex)
        {
            throw new Exception(ex.Message, ex);

        }

        finally
        {
            myConn.Close();
            adapt.Dispose();
            ds.Dispose();
            myConn.Dispose();
            
        }

    }

}
